function scaledat(data)
%
% scale the data
%
global CURRENT VOLTAGE WCHAN ALLCH
global DFILE

MASK = hex2dec('fff');	% lower bit mask for the logic information
ZERO  = 2048;
% intialize the output arguments.
CURRENT=[];
VOLTAGE=[];
WCHAN=[];

%reference gains
pts=DFILE.nr_points;
if(DFILE.mode == 3)
    refvg=DFILE.refvg*10;			%ch1 internal reference gain
else
    refvg = DFILE.refvg*10;
end
refwg=DFILE.refwg;
refig=DFILE.refig;
DD_CAL=2.50122;		%digidata gain (mode 3 only)
MB_CAL = 2.441406; % Manis/Bertrand board (mode 2 only)
%calibrate the data
switch DFILE.mode
    case {0, 1}
        %voltage cha 1
        VOLTAGE(:,1:pts)  = (bitand(floor(data(:,1:pts)), MASK) - ZERO)/refvg*MB_CAL/DFILE.vgain+DFILE.junctionpot;
        %current cha 2 in nA (thats why /1000)
        CURRENT(:,1:pts)  = (bitand(floor(data(:,pts+1:2*pts)), MASK) - ZERO)/refig*MB_CAL/DFILE.igain;
        %channel 3 gain
        if(DFILE.nr_channel == 3)
            WCHAN(:,1:pts) = (bitand(floor(data(:,2*pts+1:3*pts)), MASK) - ZERO)*MB_CAL/DFILE.wgain;
        end


    case 3
        if(isempty(DFILE.junctionpot))
            DFILE.junctionpot = 0;
        end;
        %voltage cha 1
        VOLTAGE(:,1:pts)  = floor(data(:,1:pts)/16)/refvg*DD_CAL/DFILE.vgain+DFILE.junctionpot;
        %current cha 2 in nA (thats why /1000)
        CURRENT(:,1:pts)  = floor(data(:,pts+1:2*pts)/16)/refig*DD_CAL/DFILE.igain;
        %channel 3 gain
        ALLCH = cell(DFILE.nr_channel, pts);
        ALLCH{1} = VOLTAGE;
        ALLCH{2} = CURRENT;
        if(DFILE.nr_channel == 3)
            WCHAN(:,1:pts) = floor(data(:,2*pts+1:3*pts)/16)*DD_CAL/DFILE.wgain;
            ALLCH{3} = WCHAN;
        end
        
    case 9 % special (and new, 7/07) floating point mode
        if(isempty(DFILE.junctionpot))
            DFILE.junctionpot = 0;
        end;
        %voltage cha 1
        VOLTAGE(:,1:pts)  = data(:,1:pts)/refvg/DFILE.vgain+DFILE.junctionpot;
        %current cha 2 in nA (thats why /1000)
        CURRENT(:,1:pts)  = data(:,pts+1:2*pts)/refig/DFILE.igain;
        %channel 3 gain
        ALLCH = cell(DFILE.nr_channel, pts);
        ALLCH{1} = VOLTAGE;
        ALLCH{2} = CURRENT;
        if(DFILE.nr_channel == 3)
            WCHAN(:,1:pts) = data(:,2*pts+1:3*pts)/DFILE.wgain;
            ALLCH{3} = WCHAN;
        end
        
    case 2
        %voltage cha 1
        VOLTAGE(:,1:pts)  = (bitand(floor(data(:,1:pts)), MASK) - ZERO)/refvg*MB_CAL/DFILE.vgain+DFILE.junctionpot;
        %current cha 2 in nA (thats why /1000)
        CURRENT(:,1:pts)  = (bitand(floor(data(:,pts+1:2*pts)), MASK) - ZERO)/refig*MB_CAL/DFILE.igain;
        %channel 3 gain
        if(DFILE.nr_channel == 3)
            WCHAN(:,1:pts) = (bitand(floor(data(:,2*pts+1:3*pts)), MASK) - ZERO)*MB_CAL/DFILE.wgain;
        end

    case 5 % matlab file...
        %voltage cha 1
        p = (1:pts);
        VOLTAGE  = data(:,p) + DFILE.junctionpot;
        %current cha 2 in nA (thats why /1000)
        CURRENT  = data(:,p+pts);
        %channel 3 gain
        nch = max(DFILE.nr_channel);
        if(nch == 3)
            WCHAN = data(:,p+2*pts);	% WCHAN contains ALL channels.
        end
        ALLCH = cell(nch); %, size(DFILE.nr_channel, 2), pts);
        p = (1:pts);
        for j = 1:nch
            ALLCH{j} = data(:,p+(j-1)*pts);
        end;

    case 6 % abf format: data is already scaled in readepisode.
        if(min(size(data)) == 2) % at least 2 channels.
            VOLTAGE(:,1:pts) = data(:,1:pts)+DFILE.junctionpot;
            CURRENT(:,1:pts) = data(:,pts+1:2*pts);
        else
            switch(DFILE.dmode)
                case 'VC' % one channel : assume data is current trace
                    CURRENT(:,1:pts) = data(:)';
                    VOLTAGE = []; % clear it out....
                case 'CC' % one channel: assume data is voltage trace
                    VOLTAGE(:,1:pts) = data(:)';
                    CURRENT = []; % clear it out...
                otherwise % assume it is like current clamp
                    VOLTAGE(:,1:pts) = data(:)';
                    CURRENT = []; % clear it out. (takes less space);
            end;
        end;

    otherwise
        QueMessage('scaledat: Unrecognized data structure (DFILE.mode)', 1);
end;
return

